<script setup lang='ts'>
import { accessToken, userCurrentUserInfo } from '@fl/api/bidding-api'
import { useUserStore } from '@fl/store/modules/user'
import { getEnv } from '@fl/utils/env'
import Cookie from 'js-cookie'

import Footer from '../footer/footer.vue'
import IssureIcon from './components/IssureBox.vue'
import List from './components/list.vue'

import tenderBg from '/assets/home/tender-bg.png'

const { query } = useRoute()
const { refetch } = userCurrentUserInfo(false)

const centerDialogVisible = ref(false)
const VITE_STORE_PREFIX = 'AnJuMall'
// export function getCommonStoragePrefix() {
//     return `${import.meta.env.VITE_STORE_PREFIX.replace(/\s/g, '_')}__${getEnv()}`
// }
const enterpriseAgreenKey = 'enterpriseAgreen'
const { push } = useRouter()
const userStore = useUserStore()
console.log(query, 'query')
if (query && query.token) {
    const malldev = `${VITE_STORE_PREFIX.replace(/\s/g, '_')}__${getEnv()}`
    const mallAccessToken = `${malldev}__access_token`
    Cookie.set(accessToken, query.token.toString())
    Cookie.set(mallAccessToken, query.token.toString())
    try {
        localStorage.setItem(accessToken, query.token.toString())
        localStorage.setItem(mallAccessToken, query.token.toString())
    }
    catch (e) {
        console.warn('localStorage is unavailable, using sessionStorage')
        sessionStorage.setItem(accessToken, query.token.toString())
        sessionStorage.setItem(mallAccessToken, query.token.toString())
    }
}
else {
    centerDialogVisible.value = !!(userStore.userInfo && (userStore!.userInfo as any)!.userTypeCategory === '游客' && sessionStorage.getItem(enterpriseAgreenKey) !== String(true))
}

if (Cookie.get(accessToken) && Cookie.get(accessToken) !== '') {
    refetch()
}

function enterPrise() {
    centerDialogVisible.value = false

    sessionStorage.setItem(enterpriseAgreenKey, String(true))
    push('/authentication/agreement')
}
// function handleMessage(event) {
//     const tokenData = JSON.parse(JSON.stringify(event.data))
//     console.log(tokenData, 'tokenData')

//     if (tokenData.type === 'setToken') {
//         Cookie.set(accessToken, tokenData.token)
//         window.parent.postMessage({
//             type: 'close',
//         }, event.origin)
//     }
// }
// const token = Cookie.get(accessToken)
// console.log(token)

// if (token && token !== '') {
//     refetch()
// }
// window.addEventListener('message', handleMessage, false)
</script>

<template>
    <div class="h-221">
        <el-image class="h-[100%] w-[100%]"
                  :src="tenderBg"
        />
    </div>

    <IssureIcon />

    <List />

    <Footer />

    <el-dialog v-model="centerDialogVisible"
               title="企业认证"
               width="500"

               center
               destroy-on-close
    >
        <span>
            企业认证后才能参与投标报名
        </span>

        <template #footer>
            <div class="dialog-footer">
                <el-button @click="centerDialogVisible = false">
                    取消
                </el-button>

                <el-button type="primary"
                           @click="enterPrise"
                >
                    企业认证
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
